/*
 * DDriver.java
 *
 * Created on 20 de abril de 2007, 05:21 PM
 */

package ig.metrica.ui.conexion;

import ig.util.LoadProperties;
import java.util.Properties;

/**
 *
 * @author  jsanchez
 */
public class DDriver extends javax.swing.JDialog {
    PantallaDatosBasicosConexion datosbasicos=null;
    private Properties prop=LoadProperties.loadPropertiess("server/odatos/odatos.properties");
    /** Creates new form DDriver */
    public DDriver(java.awt.Frame parent, boolean modal,PantallaDatosBasicosConexion datosbasicos) {
        super(parent, modal);
        initComponents();
        this.datosbasicos=datosbasicos;
        loadDriver();
        this.setTitle("Conexiones");
        centrar();
        setVisible(true);
    }
    
    private void loadDriver(){
        int i=0;
        while(prop.getProperty("name"+i)!=null){
            DriverDTO dto=new DriverDTO(prop.getProperty("name"+i).toString(),prop.getProperty("driver"+i).toString(),prop.getProperty("url"+i).toString());
            dto.setDatos(prop.getProperty("datos"+i)!=null?(prop.getProperty("datos"+i).equals("true")?true:false):false);
            dto.setLablePass(prop.getProperty("labelPassword"+i));
            dto.setLableUsuario(prop.getProperty("labelUsuario"+i));
            this.CBDriver.addItem(dto);
            i++;
        }
    }
    public void centrar() {
        java.awt.Dimension dimensionScreen = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Rectangle dimensionPantalla = getBounds();
        setLocation((dimensionScreen.width - dimensionPantalla.width) / 2, (dimensionScreen.height - dimensionPantalla.height) / 2);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        host = new javax.swing.JTextField();
        DB = new javax.swing.JTextField();
        Aceptar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        CBDriver = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        jLabel1.setText("Host");

        jLabel2.setText("Base de Datos");

        host.setText("localhost");

        DB.setText("MyDB");

        Aceptar.setText("Aceptar");
        Aceptar.setSelected(true);
        Aceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AceptarActionPerformed(evt);
            }
        });

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        CBDriver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CBDriverActionPerformed(evt);
            }
        });

        jLabel3.setText(org.openide.util.NbBundle.getMessage(DDriver.class, "DDriver.jLabel3.text")); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(CBDriver, 0, 226, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, DB, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, host, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 159, Short.MAX_VALUE)
                        .add(Aceptar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 81, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(CBDriver, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(host, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(DB, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(Aceptar)
                    .add(jButton1))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void CBDriverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CBDriverActionPerformed
        if(CBDriver.getSelectedItem()!=null && ((DriverDTO)CBDriver.getSelectedItem()).isDatos()){
            this.DB.setEnabled(true);
            this.host.setEnabled(true);
        }else{
            this.DB.setEnabled(false);
            this.host.setEnabled(false);
        }
    }//GEN-LAST:event_CBDriverActionPerformed
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void AceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AceptarActionPerformed
// TODO add your handling code here:
        if(this.CBDriver.getSelectedItem()!=null ){
            datosbasicos.setNombre(((DriverDTO)this.CBDriver.getSelectedItem()).getName());
            datosbasicos.setDriver(((DriverDTO)this.CBDriver.getSelectedItem()).getDriver());
            String URL = ((DriverDTO)this.CBDriver.getSelectedItem()).getURL();
            if(((DriverDTO)this.CBDriver.getSelectedItem()).isDatos()){
                URL =URL.replaceAll("HOST",this.host.getText());
                URL =URL.replaceAll("DB",this.DB.getText());
            }
            datosbasicos.setURL(URL);
            datosbasicos.setUsuario("");
            datosbasicos.setPassword("");
            datosbasicos.setNameLable(((DriverDTO)this.CBDriver.getSelectedItem()).getLableUsuario(),((DriverDTO)this.CBDriver.getSelectedItem()).getLablePass());
        }
        this.dispose();
    }//GEN-LAST:event_AceptarActionPerformed
    
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new DDriver(new javax.swing.JFrame(), true).setVisible(true);
//            }
//        });
//    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Aceptar;
    private javax.swing.JComboBox CBDriver;
    private javax.swing.JTextField DB;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JTextField host;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
    
}
